package com.tanhua.houtai.server.controller;


import cn.hutool.http.HttpResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.tanhua.domain.db.SoulQuestion;
import com.tanhua.domain.db.SoulTest;
import com.tanhua.houtai.server.service.impl.XlsxServiceImpl;
import org.apache.coyote.Response;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/xslx")
public class XlsxControlller {
    @Autowired
    private XlsxServiceImpl xlsxService;
    @PostMapping("/save")
    public View saveSoulTest(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String[] split = originalFilename.split("\\.");
        System.out.println(Arrays.asList(split));
        String s = split[1];
        String back="";
        if("xlsx".equals(s)){
            Workbook workbook=new XSSFWorkbook(file.getInputStream());
            back = back+xlsxService.saveXlsx(workbook);
        }else if("xls".equals(s)){
            Workbook workbook=new HSSFWorkbook(file.getInputStream());
            back = back+ xlsxService.saveXlsx(workbook);
        }
        View view=new RedirectView("/questionController.html");
        return view;
    }
    @RequestMapping("/getquestion")
    public Page getall(@RequestBody HashMap<String,Object> map){
        int page = (int) map.get("current");
        int pagesize = (int) map.get("size");
        String name = map.get("name").toString();
        String question = map.get("question").toString();
        Page getsoul = xlsxService.getsoul(page, pagesize,name,question);
        return getsoul;
    }
    @RequestMapping("/deleteoptions")
    public void deleteOne(String id){
        //xlsxService.deleteOne(id);
        System.out.println("id为："+id);
    }

    @RequestMapping("/updatehide")
    public void updatehide(String id){
        xlsxService.updatehide(id);

    }

    @RequestMapping("/updateoption")
    public void updateoption(@RequestBody HashMap<String,Object> map){
        xlsxService.updateoption(map);
    }

    @RequestMapping("/getallsoul")
    public List<SoulTest> getAllSoul(){
      List<SoulTest> list= xlsxService.getallsoul();
      return list;
    }

    @RequestMapping("/getallquestion")
    public List<SoulQuestion> getAllQuestion(Integer id){
        List<SoulQuestion> list= xlsxService.getallquestion(id);
        return list;
    }

    @RequestMapping("/deletequestion")
    public void deletequestion(@RequestParam(value = "id",required = false) String id,@RequestParam(value = "qid",required = false) String qid){
        System.out.println(id+"=="+qid);
        xlsxService.deleteChoice(id,qid);
    }
    @RequestMapping("/downloadexcel")
    public void downloadexcel(HttpServletResponse response) throws IOException {
        response.setHeader("content-disposition","attachment;filename=contract.xlsx");
        Workbook byDate = xlsxService.getExcel();
        byDate.write(response.getOutputStream());
    }
}
